package com.wushi.lenovo.asia5b.app_ui.other.dialog;

import android.arch.lifecycle.ViewModelProviders;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mvp.base.BaseDialogFragment;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_event.SelectVoucherEvent;
import com.wushi.lenovo.asia5b.app_model.voucher.VoucherModel;
import com.wushi.lenovo.asia5b.app_ui.other.dialog.vm.SelectVoucherViewModel;
import com.wushi.lenovo.asia5b.app_ui.voucher.adapter.SelectVoucherAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by zhangyuncai on 2019/11/13.
 */
public class SelectVoucherDialogFragment extends BaseDialogFragment {
    @BindView(R.id.recyclerview_voucher)
    RecyclerView mRecyclerView;
    final List<VoucherModel> mDatas = new ArrayList<>();
    private SelectVoucherAdapter mAdapter;
    SelectVoucherViewModel mViewModel;

    @Override
    protected int getLayoutId() {
        mViewModel = ViewModelProviders.of(this).get(SelectVoucherViewModel.class);
        mViewModel.setLoadMvpView(this);
        return R.layout.df_selectvoucher;
    }

    @Override
    protected void initView() {
        mAdapter = new SelectVoucherAdapter(mDatas);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                VoucherModel model = mAdapter.getItem(position);
                EventBus.getDefault().post(new SelectVoucherEvent(model));
                dismiss();
            }
        });
    }

    @Override
    protected void initData() {
        mViewModel.getVoucherList(listResultModel -> {
            if (listResultModel.isSuccess() && listResultModel.getData() != null) {
                mDatas.clear();
                mDatas.addAll(listResultModel.getData());
                mAdapter.notifyDataSetChanged();
            }

            if (mDatas.size() == 0) {
                mAdapter.setEmptyView(View.inflate(getContext(), R.layout.empty_voucher2, null));
            }

        });
    }

    @Override
    protected void initEvent() {

    }

    @Override
    @OnClick({
            R.id.iv_cancelsure_ok
    })
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_cancelsure_ok:
                dismiss();
                break;
        }
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getWidthStyle()  {
        return getScreenWidth(0.9f);
    }
}
